package com.ktjy.ch15.controller;


import com.ktjy.ch15.entity.MeetinGroom;
import com.ktjy.ch15.service.MeetinGroomService;
import com.ktjy.ch15.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/main/meetingroom")
public class MeetinGroomController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(MeetinGroomController.class);

    @Autowired
    private MeetinGroomService meetinGroomService;

    @RequestMapping("/page")
    public String page(Model model, MeetinGroom condition,
                       @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        Page<MeetinGroom> page = null;
        try {
            // 查询用户的分页信息
            page = meetinGroomService.findPage(condition, pageNo, pageSize);
            // 查询所有的角色信息
            // 将分页数据、角色列表和查询条件设置到模型数据中
            model.addAttribute("pageInfo", page);
            model.addAttribute("condition", condition);
        } catch (Exception e) {
            LOGGER.error("MeetinGroomController 中的 page 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "meetingroom/page";
    }

    @GetMapping("/save")
    public String toSave(Model model) {
        try {
            // 回显数据时需要 sysUser，这里给个空对象避免出错
            model.addAttribute("meetinGroom", new MeetinGroom());
        } catch (Exception e) {
            LOGGER.error("MeetinGroomController中的toSave方法执行发生错误。", e);
            throw new RuntimeException(e);
        }
        return "meetingroom/save";
    }

    @PostMapping("/save")
    public String doSave(MeetinGroom meetinGroom) {
            try {
                boolean ok =false;
                if(meetinGroom.getId()==null){//新增
                    ok = meetinGroomService.save(meetinGroom);
                }else{//修改
                    ok= meetinGroomService.update(meetinGroom);
                }

                if(ok){
                    //跳转到列表;重定向到分页方法
                    return redirect("/main/meetingroom/page");
                }else{
                    //跳回新增页面
                    return "meetingroom/save";
                }
            } catch (Exception e) {
                LOGGER.error("MeetinGroomController 中的 save 方法执行发生错误。{} ",e);
                throw new RuntimeException(e);
            }
    }

    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id, Model model){
        MeetinGroom meetinGroom = null;
        try {
            meetinGroom = meetinGroomService.findById(id);
            model.addAttribute("meetinGroom", meetinGroom);
        } catch (Exception e) {
            LOGGER.error("MeetinGroomController 中的 view 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "meetingroom/view";
    }

    @GetMapping("/edit")
    public String toEdit(@RequestParam("id") Long id, Model model) {
        MeetinGroom meetinGroom = null;
        try {
            meetinGroom = meetinGroomService.findById(id);
            // 回显数据时需要 supplier，这里给个空对象避免出错
            model.addAttribute("meetinGroom", meetinGroom);
        } catch (Exception e) {
            LOGGER.error("MeetinGroomController 中的 toEdit 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "meetingroom/save";
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Object del(@PathVariable("id") Long id) {
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            boolean ok = meetinGroomService.delete(id);
            if (ok){
                map.put("code",200);
                map.put("msg","删除成功");
            }else {
                map.put("code",500);
                map.put("msg","删除失败");
            }
        }catch (Exception e){
            LOGGER.info("发生错误",e);
            map.put("code",500);
            map.put("msg","发生异常");
        }
        return map;
    }

    @GetMapping("/nameExist")
    @ResponseBody
    public Object nameExist(@RequestParam("name") String name) {
        Map<String,Object> map = new HashMap<>();
        try {
            MeetinGroom meetinGroom = meetinGroomService.findByName(name);
            if (meetinGroom != null){
                map.put("exist",1);
            }else {
                map.put("exist",0);
            }
        }catch (Exception e){
            LOGGER.error("MeetinGroomController中的nameExists方法执行发生错误", e);
            map.put("exist",-1);
            map.put("error", "出现异常，请联系管理员处理！ ");
        }
        return map;
    }
}
